var Plug = {
    plug_in: function (begType, dataUrl, pageId, placeholder, Callback, showVal, defaultParentCode, successed, initId) {
        /*
         * begType:数据请求方式
         * dataUrl： 数据接口地址；
         * pageId：容器Id；
         * placeholder：自定义placeholder
         * Callback：最后一级时回调函数
         * showVal:显示值方式：false为只显示当前级所选值，true为每一级值都显示
         * */
        $(pageId).addClass("bus-plugIn");
        $.ajax({
            type: begType,
            url: dataUrl,
            dataType: "json",
            success: function (data) {
                if (data.code != '0') {
                    return;
                }
                var dataObj = data.data;
                var newArr = {};
                var firstHtml = "";
                var plugArr = [];
                var flag = false;
                pageId = pageId + " ";
                $(pageId + ".bus-picker").children(".bus-picker-content").eq(0).show(); //默认显示第一级
                $(pageId + ".bus-picker-tab").before('<span class="empty">清空</span>');
                $.each(dataObj, function (index, item) {
                    if (item.code == initId) {
                        $(pageId + ".chose-plugIn").val(item.name);
                        $(pageId + ".plug-in-msg").val(item.id);
                        $(pageId + ".plug-in-code").val(item.code);
                    }
                    var parentCode = item.pcode;//父级Id
                    var id = item.id;
                    newArr[id] = item;
                    if (parentCode == "" || parentCode == null || parentCode == defaultParentCode) {
                        firstHtml += '<a href="javascript:void(0);" data-id =' + item.id + ' data-code =' + item.code + '>' + item.name + '</a>'
                        $(pageId + ".bus-picker-content").html(firstHtml);
                    }
                });
                $(pageId).undelegate(".bus-picker-content a", "click");
                $(pageId).delegate(".bus-picker-content a", "click", function () { //跳转下一级
                    var that = $(this),
                        sortNum = $(this).attr("data-id"), //获取父级Id
                        sortCode = $(this).attr("data-code"),//获取code
                        BusSort = $(this).text(),
                        nextFloor = {};
                    $(pageId + ".bus-picker-tab a.active").text(BusSort);
                    $(this).parent().find("a").removeClass("active");
                    $(this).addClass("active");
                    $.each(newArr, function (index, item) {
                        if (item.pid == sortNum) {
                            nextFloor[index] = item;
                        }
                    });
                    var tl = $(pageId + ".bus-picker-content").length;
                    if (JSON.stringify(nextFloor) != "{}") {
                        that.parent().after('<div class="bus-picker-content"></div>');
                        $(pageId + ".bus-picker-tab a.active").removeClass("active");
                        $(pageId + ".bus-picker-tab").append('<a href="javascript:void(0);" class="active">' + placeholder + '</a>');
                        $.each(dataObj, function (index, item) {
                            if (item.pid == sortNum) {
                                var nextHtml = "";
                                nextHtml += '<a href="javascript:void(0);" data-id =' + item.id + ' data-code =' + item.code + '>' + item.name + '</a>';
                                that.parent().next(pageId + ".bus-picker-content").append(nextHtml);
                            }
                        });

                        plugArr.push(BusSort);
                        var endResult = '';
                        $.each(plugArr, function (index, item) {
                            if (index == 0) {
                                endResult += item;
                            } else {
                                endResult += " / " + item;
                            }
                        });
                        if (showVal == true) {
                            $(pageId + ".chose-plugIn").val(endResult);
                            $(pageId + ".plug-in-msg").val(sortNum);
                            $(pageId + ".plug-in-code").val(sortCode);
                        } else {
                            if (tl >= 2) {
                                $(pageId + ".chose-plugIn").val(endResult);
                                $(pageId + ".plug-in-msg").val(sortNum);
                                $(pageId + ".plug-in-code").val(sortCode);
                            } else {
                                $(pageId + ".chose-plugIn").val("");
                                $(pageId + ".plug-in-msg").val("");
                                $(pageId + ".plug-in-code").val(sortCode);
                            }
                        }
                        that.parent().hide();
                        that.parent().next().show();

                    } else {
                        $(".close-other").remove();

                        $(pageId + ".bus-picker").hide();
                        var endResult = '';
                        $.each(plugArr, function (index, item) {
                            if (index == 0) {
                                endResult += item;
                            } else {
                                endResult += " / " + item;
                            }
                        });
                        if (showVal == true) {
                            $(pageId + ".plug-in-msg").val(sortNum);
                            $(pageId + ".plug-in-code").val(sortCode);
                            if (plugArr.length == 0) {
                                $(pageId + ".chose-plugIn").val(endResult + BusSort);
                            } else {
                                $(pageId + ".chose-plugIn").val(endResult + ' / ' + BusSort);
                            }
                        } else {
                            if (tl >= 2) {
                                $(pageId + ".chose-plugIn").val(endResult + ' / ' + BusSort);
                                $(pageId + ".plug-in-msg").val(sortNum);
                                $(pageId + ".plug-in-code").val(sortCode);
                            } else {
                                $(pageId + ".chose-plugIn").val("");
                                $(pageId + ".plug-in-msg").val("");
                                $(pageId + ".plug-in-code").val(sortCode);
                            }
                        }
                    }
                    if (Callback == "" || Callback == undefined) {
                    } else {
                        Callback($($.trim(pageId)));
                    }
                });
                $(pageId + ".bus-picker-tab").on("click", "a", function () {
                    var tabNum = $(pageId + ".bus-picker-tab a").index(this);
                    var t = $(this),
                        tl = $(pageId + ".bus-picker-content").length;
                    $(pageId + ".bus-picker-tab a").removeClass("active");
                    $(this).addClass("active");
                    $(pageId + ".bus-picker-content").hide();
                    $(pageId + ".bus-picker-content").eq(tabNum).show();
                    var len = plugArr.length;
                    plugArr.splice(tabNum, len - tabNum);
                    flag = true;
                    $(pageId + ".bus-picker-content").on("click", "a", function () {
                        t.nextAll().remove();
                        $(pageId + ".bus-picker-content").eq(tabNum).nextAll().remove();

                    });
                });
                /**
                 *清空数据
                 * */
                $(pageId + ".empty").on("click", function () {
                    var empty = $(pageId);
                    empty.find(".bus-picker").children(".bus-picker-content").hide();
                    empty.find(".bus-picker").children(".bus-picker-content").eq(0).show();
                    empty.find(".bus-picker").children(".bus-picker-content").eq(0).find("a").removeClass("active");
                    empty.find(".bus-picker-tab a").eq(0).text("" + placeholder + "").trigger("click").nextAll().remove();
                    empty.find(".chose-plugIn").val("");
                    empty.find(".plug-in-msg").val("");
                    empty.find(".plug-in-code").val("");
                })

                if (typeof successed == 'function')
                    successed(dataObj);
            }
        });
    }
}
$(function () {
//	Choose();
    $(".chose-plugIn").on("click ", function () {
        var thatVal = $(this).val();
        var that = $(this);
        that.parent().find(".bus-picker").children(".bus-picker-content").eq(0).show();
        that.parent().find(".bus-picker").children(".bus-picker-content").eq(0).nextAll().remove();
        that.parent().find(".bus-picker-tab a").eq(0).text("").trigger("click").nextAll().remove();
        if (thatVal == "" || thatVal == null) {
            that.parent().find(".bus-picker-tab a").last().text("请选择");
        } else {
            var thatArr = $(this).val().split("/");
            var thatArrL = thatArr.length;
            var i = 0;
            $.each(thatArr, function (index, value) {
                that.parent().find(".bus-picker-content a").each(function () {
                    i++;
                    var thatTxt = $(this).text();
                    if ($.trim(thatTxt) == $.trim(value)) {
                        $(this).addClass("active");
                        if (i < thatArrL) {
                            $(this).trigger("click");
                        } else {
                            that.parent().find(".bus-picker-tab a").last().text(thatTxt);
                        }
                    }
                })
            })
        }
        $(".bus-picker").hide();
        $(this).next(".bus-picker").show();
    });
    $(document).click(function (e) {
        var blankL = $(e.target).parents(".bus-picker").length;
        var inputHas = $(e.target).hasClass("chose-plugIn");
        if (blankL == 0 && inputHas == false) {
            $(".bus-picker").hide();
        }
    });
})

/**
 * 业态选择js
 * **/
function Choose() {
    $(document).on('.chose-plugIn', "click", function (e) {
        var thatVal = $(this).val();
        var that = $(this);
        if (thatVal != "" || thatVal != null) {
            var thatArr = $(this).val().split("/");
            $.each(thatArr, function (index, value) {
                that.parent().find(".bus-picker-content a").each(function () {
                    var thatTxt = $(this).text();
                    if (thatTxt == value) {
                        $(this).addClass("active");
                        $(this).trigger("click");
                    }
                })
            })
        }
        $(".bus-picker").hide();
        $(this).next(".bus-picker").show();
    });
}